package com.fenghongzhang.day011;

import android.content.ContentResolver;
import android.content.Context;
import android.database.ContentObserver;
import android.database.Cursor;
import android.os.Handler;
import android.os.Message;
import android.provider.Telephony;
import android.util.Log;
import android.widget.Toast;

public class MyObverse extends ContentObserver {

    private Context context;
    private static final String TAG = "MyObverse";
    private Handler handler;
    public MyObverse(Handler handler,Context context) {
        super(handler);
        this.handler = handler;
        this.context = context;
    }
    @Override
    public void onChange(boolean selfChange) {
        super.onChange(selfChange);

        Log.i(TAG, "onChange: ");
        //读取第一条信息. 把值给页面中的输入框

        ContentResolver contentResolver = context.getContentResolver();

        Cursor query = contentResolver.query(Telephony.Sms.CONTENT_URI, new String[]{Telephony.Sms.BODY,Telephony.Sms.ADDRESS}, null, null, null);
        if(query != null){
            query.moveToFirst();
            String body = query.getString(query.getColumnIndex(Telephony.Sms.BODY));

            Message obtain = Message.obtain();
            obtain.obj = body;
            handler.sendMessage(obtain);

            query.close();
        }

    }
}
